package com.whmnx.chitchat.ui.book;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.classic.adapter.CommonRecyclerAdapter;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.adapter.DepAdapter;
import com.whmnx.chitchat.adapter.DepartmentAdapter;
import com.whmnx.chitchat.adapter.UserAdpter;
import com.whmnx.chitchat.base.BaseActivity;
import com.whmnx.chitchat.bean.DepartmentDean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.http.CallBack;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.ui.chat.ChatActivity;
import com.whmnx.chitchat.utils.SPUtil;
import com.whmnx.chitchat.utils.UItils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author : huang
 * date   : 2020/7/28 0028 16:41
 * version: 1.0
 *
 * @author Administrator
 */
public class DepartmentActivity extends BaseActivity {
    @BindView(R.id.rl_dep)
    RecyclerView rlDep;
    @BindView(R.id.rl_user)
    RecyclerView rlUser;
    @BindView(R.id.tv_people)
    TextView tvPeople;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.department_activity);
    }

    @Override
    protected void loadData() {
        rlDep.setLayoutManager(new LinearLayoutManager(this));
        rlDep.setItemAnimator(new DefaultItemAnimator());
        DepartmentAdapter depAdapter = new DepartmentAdapter(this);
        rlDep.setAdapter(depAdapter);

        rlUser.setLayoutManager(new LinearLayoutManager(this));
        rlUser.setItemAnimator(new DefaultItemAnimator());
        UserAdpter userAdpter = new UserAdpter(this);
        rlUser.setAdapter(userAdpter);


        String OrganizeId = getIntent().getStringExtra("OrganizeId");
        String OrgName = getIntent().getStringExtra("name");
        String DepartmentId = getIntent().getStringExtra("DepartmentId");
        boolean isTopChat = getIntent().getBooleanExtra("isTopChat",false);
        setTitle(OrgName);
        Map<String, Object> params = new HashMap<>();
        params.put("OrganizeId", OrganizeId);
        if (TextUtils.isEmpty(DepartmentId)){
            params.put("DepartmentID", "");
        }else {
            params.put("DepartmentID", DepartmentId);
        }

        HttpClient.get(this, Constants.GETDEPARTMENTLIST, params, new CallBack<DepartmentDean>() {
            @Override
            public void onSuccess(DepartmentDean result) {
                depAdapter.addAll(result.getDepartment());
                userAdpter.addAll(result.getUser());
                if (result.getUser().size() == 0){
                    tvPeople.setVisibility(View.GONE);
                }
            }
        });

        depAdapter.setOnItemClickListener(new CommonRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int i) {
                startActivity(new Intent(DepartmentActivity.this, DepartmentActivity.class)
                        .putExtra("OrganizeId", depAdapter.getItem(i).getOrganizeId())
                        .putExtra("DepartmentId", depAdapter.getItem(i).getDepartmentId())
                        .putExtra("name", depAdapter.getItem(i).getDepartmentName())
                        .putExtra("isTopChat",false));
            }
        });

        userAdpter.setOnItemClickListener(new CommonRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int i) {
               String userId = SPUtil.getString(Constants.USERID);
                if (userId.equals(userAdpter.getItem(i).getUserId())){
                    UItils.showToastSafe("自己不能和自己聊天");
                    return;
                }
                ChatInfo chatInfo = new ChatInfo();
                chatInfo.setType(TIMConversationType.C2C);
                chatInfo.setId(userAdpter.getItem(i).getUserId());
                if (isTopChat){
                    chatInfo.setTopChat(true);
                }else {
                    chatInfo.setTopChat(false);
                }
                chatInfo.setChatName(userAdpter.getItem(i).getUserNick());
                Intent intent = new Intent(DepartmentActivity.this, ChatActivity.class);
                intent.putExtra(Constants.CHAT_INFO, chatInfo);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
    }
}
